﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class QuanTri_TrangBangTongHopKetQua : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsThaoTac thaotac = new clsThaoTac();
    string manam;
    string malop;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            manam = Request.QueryString["MaNam"];
            malop = Request.QueryString["MaLop"];
            if (!IsPostBack)
            {
                cls.LoadComboboxNamHoc(drNamHoc);
                cls.LoadComboboxLopHoc(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()));
                drNamHoc.SelectedValue = manam;
                drLopHoc.SelectedValue = malop;
                TongHopKetQua(int.Parse(drNamHoc.SelectedValue.ToString()), int.Parse(drLopHoc.SelectedValue.ToString()));
            }
        }
    }
    protected void drNamHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        cls.LoadComboboxLopHoc(drLopHoc,int.Parse(drNamHoc.SelectedValue.ToString()));
    }

    private void TongHopKetQua(int manam,int malop)
    {
        int stt=0;
        DataTable thkq = new DataTable();
        thkq.Columns.Add("STT", typeof(string));
        thkq.Columns.Add("MaHS",typeof(string));
        thkq.Columns.Add("TenHS",typeof(string));
        thkq.Columns.Add("DTBKI",typeof(string));
        thkq.Columns.Add("DTBKII",typeof(string));
        thkq.Columns.Add("DTBCN",typeof(string));
        thkq.Columns.Add("HLKI",typeof(string));
        thkq.Columns.Add("HKKI",typeof(string));
        thkq.Columns.Add("HLKII", typeof(string));
        thkq.Columns.Add("HKKII",typeof(string));
        thkq.Columns.Add("HLCN",typeof(string));
        thkq.Columns.Add("HKCN",typeof(string));
        thkq.Columns.Add("NghiCP",typeof(string));
        thkq.Columns.Add("NghiKP",typeof(string));
        thkq.Columns.Add("NghiCN",typeof(string));
        thkq.Columns.Add("DanhHieu",typeof(string));
        var c=from p in db.funcTraVeMaHSTrongBangLop_HS(manam,malop) select p;
        if (c.Count() != 0)
        {
            foreach (var con in c)
            {
                stt=stt+1;
                DataRow r = thkq.NewRow();
                r[0] = stt.ToString();
                r[1] = con.MaHS;
                r[2] = con.TenHS;
                var d=from p in db.funcTraVeDTBChungCuaHocKy(manam,1,con.MaHS) select p;
                var d1=from p in db.funcTraVeDTBChungCuaHocKy(manam,2,con.MaHS) select p;
                if (d.Count() != 0)
                {
                    r[3] = d.First().DiemTongKet;
                    r[7] = d.First().HanhKiem;
                }
                else { r[3] = ""; r[7] = ""; }
                if (d1.Count() != 0)
                {
                    r[4] = d1.First().DiemTongKet;
                    r[9] = d1.First().HanhKiem;
                }
                else { r[4] = ""; r[9] = ""; }
                if(r[9].ToString()!="")
                { r[11] = r[9].ToString(); }
                if (r[9].ToString() == "")
                { r[11] = ""; }
                if (r[3].ToString() != "")
                {
                    r[6] = DanhGiaHL(manam, malop, 1, r[1].ToString(), float.Parse(r[3].ToString()));
                }
                else { r[6] = ""; }
                if (r[4].ToString() != "")
                {
                    r[8] = DanhGiaHL(manam, malop, 1, r[1].ToString(), float.Parse(r[4].ToString()));
                }
                else { r[8] = ""; }
                if (r[3].ToString() != "" && r[4].ToString() != "")
                {
                    string hl;
                    r[5] = TinhDiemTKCN(manam, con.MaHS, out hl);
                    r[10] = hl;
                }
                else { r[5] = ""; r[10] = ""; }
                r[12] = "";
                r[13] = "";
                r[14] = "";
                r[15] = "";
                thkq.Rows.Add(r);
            }
            grvBangTongHopKQ.DataSource = thkq;
            grvBangTongHopKQ.DataBind();
        }
    }
    private string DanhGiaHL(int manam,int malop,int maky,string mahs,float dtb)
    {
        string hl = ""; 
        var toan1 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 1, mahs) select p.DTBMonHocKy;
        //string dtoan = toan1.First().ToString();
        //Response.Write(dtoan);
        var van1=from p in db.funcTraVeDTBCuaTungMonHoc(manam,maky,6,mahs) select p.DTBMonHocKy;
        if (toan1.Count() != 0 && van1.Count() != 0)
        {
            if (toan1.First() != null)
            {
                float toan = float.Parse(toan1.First().ToString());

                if (van1.First() != null)
                {
                    float van = float.Parse(van1.First().ToString());

                    var c = from p in db.funcLayDTBTatCaCacMonCuaHocsinhHu(manam, malop, maky, mahs) select p.DTBMonHocKy;
                    if (dtb >= 8.0)
                    {
                        if (toan >= 8.0 || van >= 8.0)
                        {
                            int tong = 0;
                            foreach (var con in c)
                            {
                                if (con != null)
                                {
                                    if (con <= 6.4)
                                    {
                                        tong = tong + 1;
                                    }
                                }
                            }
                            if (tong == 0)
                            {
                                hl = "Giỏi";
                            }
                            if (tong != 0)
                            { hl = "Khá"; }

                        }
                        if (toan <= 7.9 && van <= 7.9)
                        {
                            hl = "Khá";
                        }

                    }
                    if (dtb >= 6.5 && dtb < 8.0)
                    {
                        if (toan >= 5.0 || van >= 5.0)
                        {
                            int tong = 0;
                            foreach (var con in c)
                            {
                                if (con != null)
                                {
                                    if (con <= 4.9)
                                    {
                                        tong = tong + 1;
                                    }
                                }
                            }
                            if (tong == 0)
                            {
                                hl = "Khá";
                            }
                            if (tong != 0)
                            { hl = "TB"; }

                        }
                        if (toan <= 4.9 && van <= 4.9)
                        {
                            hl = "TB";
                        }
                    }
                    if (dtb >= 5.0 && dtb < 6.5)
                    {
                        if (toan >= 3.5 || van >= 3.5)
                        {
                            int tong = 0;
                            foreach (var con in c)
                            {
                                if (con != null)
                                {
                                    if (con <= 3.4)
                                    {
                                        tong = tong + 1;
                                    }
                                }
                            }
                            if (tong == 0)
                            {
                                hl = "TB";
                            }
                            if (tong != 0)
                            { hl = "Yếu"; }

                        }
                        if (toan <= 3.4 && van <= 3.4)
                        {
                            hl = "Yếu";
                        }
                    }
                    if (dtb >= 3.5 && dtb < 5.0)
                    {
                        if (toan >= 2.0 || van >= 2.0)
                        {
                            int tong = 0;
                            foreach (var con in c)
                            {
                                if (con != null)
                                {
                                    if (con <= 1.9)
                                    {
                                        tong = tong + 1;
                                    }
                                }
                            }
                            if (tong == 0)
                            {
                                hl = "Yếu";
                            }
                            if (tong != 0)
                            { hl = "Kém"; }

                        }
                        if (toan <= 1.9 && van <= 1.9)
                        {
                            hl = "Kém";
                        }
                    }
                    if (dtb < 2.0)
                    {
                        hl = "Kém";
                    }

                }
            }
        }
        return hl;

    }
    private string TinhDiemTKCN(int manam, string mahs,out string hl)
    {
        List<string> list = new List<string>();
        string dtk = ""; hl = "";
        float tongdiem=0;
        int heso=0;
        var c = from p in db.MonHocs select p.MaMon;
        //foreach (var con in c)
        //{
        //    string diem=thaotac.tmptinhDiemTBTCN(manam,int.Parse(con.ToString()),mahs,ref tongdiem,ref heso);

        //}
        string toan1 = thaotac.tmptinhDiemTBTCN(manam, 1, mahs, ref tongdiem, ref heso);
        list.Add(toan1);
        string ly = thaotac.tmptinhDiemTBTCN(manam, 2, mahs, ref tongdiem, ref heso);
        list.Add(ly);
        string hoa = thaotac.tmptinhDiemTBTCN(manam, 3, mahs, ref tongdiem, ref heso);
        list.Add(hoa);
        string sinh = thaotac.tmptinhDiemTBTCN(manam, 4, mahs, ref tongdiem, ref heso);
        list.Add(sinh);
        string tin = thaotac.tmptinhDiemTBTCN(manam, 5, mahs, ref tongdiem, ref heso);
        list.Add(tin);
        string van1 = thaotac.tmptinhDiemTBTCN(manam, 6, mahs, ref tongdiem, ref heso);
        list.Add(van1);
        string su = thaotac.tmptinhDiemTBTCN(manam, 7, mahs, ref tongdiem, ref heso);
        list.Add(su);
        string dia = thaotac.tmptinhDiemTBTCN(manam, 8, mahs, ref tongdiem, ref heso);
        list.Add(dia);
        string NN = thaotac.tmptinhDiemTBTCN(manam, 9, mahs, ref tongdiem, ref heso);
        list.Add(NN);
        string gdcd = thaotac.tmptinhDiemTBTCN(manam, 10, mahs, ref tongdiem, ref heso);
        list.Add(gdcd);
        string cn = thaotac.tmptinhDiemTBTCN(manam, 11, mahs, ref tongdiem, ref heso);
        list.Add(cn);
        string theduc = thaotac.tmptinhDiemTBTCN(manam, 12, mahs, ref tongdiem, ref heso);
        list.Add(theduc);
        string gdqp = thaotac.tmptinhDiemTBTCN(manam, 13, mahs, ref tongdiem, ref heso);
        list.Add(gdqp);
        dtk = thaotac.LamTronDiem((tongdiem / heso).ToString());
        
        if (toan1 != "" && van1 != "")
        {
            float toan = float.Parse(toan1);
            float van = float.Parse(van1);
            if (dtk != "")
            {
                float dtb = float.Parse(dtk);
                if (dtb >= 8.0)
                {
                    if (toan >= 8.0 || van >= 8.0)
                    {
                        int tong = 0;
                        for (int i = 0; i < list.Count-1;i++ )
                        {
                            if (list[i].ToString() != "")
                            {
                                if (float.Parse(list[i].ToString()) <= 6.4)
                                {
                                    tong = tong + 1;
                                }
                            }
                        }
                        if (tong == 0)
                        {
                            hl = "Giỏi";
                        }
                        if (tong != 0)
                        { hl = "Khá"; }

                    }
                    if (toan <= 7.9 && van <= 7.9)
                    {
                        hl = "Khá";
                    }

                }
                if (dtb >= 6.5 && dtb < 8.0)
                {
                    if (toan >= 5.0 || van >= 5.0)
                    {
                        int tong = 0;
                        for (int i = 0; i < list.Count - 1; i++)
                        {
                            if (list[i].ToString() != "")
                            {
                                if (float.Parse(list[i].ToString()) <= 4.9)
                                {
                                    tong = tong + 1;
                                }
                            }
                        }
                        if (tong == 0)
                        {
                            hl = "Khá";
                        }
                        if (tong != 0)
                        { hl = "TB"; }

                    }
                    if (toan <= 4.9 && van <= 4.9)
                    {
                        hl = "TB";
                    }
                }
                if (dtb >= 5.0 && dtb < 6.5)
                {
                    if (toan >= 3.5 || van >= 3.5)
                    {
                        int tong = 0;
                        for (int i = 0; i < list.Count - 1; i++)
                        {
                            if (list[i].ToString() != "")
                            {
                                if (float.Parse(list[i].ToString()) <= 3.4)
                                {
                                    tong = tong + 1;
                                }
                            }
                        }
                        if (tong == 0)
                        {
                            hl = "TB";
                        }
                        if (tong != 0)
                        { hl = "Yếu"; }

                    }
                    if (toan <= 3.4 && van <= 3.4)
                    {
                        hl = "Yếu";
                    }
                }
                if (dtb >= 3.5 && dtb < 5.0)
                {
                    if (toan >= 2.0 || van >= 2.0)
                    {
                        int tong = 0;
                        for (int i = 0; i < list.Count - 1; i++)
                        {
                            if (list[i] != null)
                            {
                                if (float.Parse(list[i]) <= 1.9)
                                {
                                    tong = tong + 1;
                                }
                            }
                        }
                        if (tong == 0)
                        {
                            hl = "Yếu";
                        }
                        if (tong != 0)
                        { hl = "Kém"; }

                    }
                    if (toan <= 1.9 && van <=1.9)
                    {
                        hl = "Kém";
                    }
                }
                if (dtb < 2.0)
                {
                    hl = "Kém";
                }
            }
        }
        return dtk;


    }
    protected void btnHienThi_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        TongHopKetQua(manam, malop);

    }
    protected void drLopHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        TongHopKetQua(manam, malop);
    }
}
